package com.bstek.uflo.form.view.common.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.htmlparser.Node;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.Component;
import com.bstek.dorado.view.widget.grid.DataColumn;
import com.bstek.dorado.view.widget.grid.Grid;
import com.bstek.dorado.web.DoradoContext;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.form.model.TableDefinition;
import com.bstek.uflo.form.view.common.AbstractComponentBuilder;
import com.bstek.uflo.model.HistoryTask;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.query.HistoryTaskQuery;
import com.bstek.uflo.service.HistoryService;
import com.bstek.uflo.service.ProcessService;

/**
 * @author Jacky.gao
 * @since 2013年11月22日
 */
@org.springframework.stereotype.Component("com.bstek.uflo.form.view.common.impl.HistoryTaskBuilder")
public class HistoryTaskBuilder extends AbstractComponentBuilder {
	
	@Autowired
	@Qualifier(HistoryService.BEAN_ID)
	private HistoryService historyService;
	
	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;
	
	@Autowired
	@Qualifier(ProcessService.BEAN_ID)
	private ProcessService processService;
	
	public boolean support(TagNode node) {
		if(node instanceof TableTag){
			TableTag table=(TableTag)node;
			String css=table.getAttribute("class");
			if(css!=null && css.equals("ufloapproveopinion")){
				return true;
			}
		}
		return false;
	}

	public void build(TagNode node, View view, Map<String, Component> map,Map<String, Boolean> mappingMap, TableDefinition table) {
		TableTag tableTag=(TableTag)node;
		NodeList trList=tableTag.getChildren();
		if(trList.size()==0)return;
		Node trNode=null;
		for(int i=0;i<trList.size();i++){
			Node n=trList.elementAt(i);
			if(n!=null && n instanceof TableRow){
				trNode=n;
				break;
			}
		}
		NodeList tdList=trNode.getChildren();
		if(tdList.size()==0)return;
		org.htmlparser.tags.TableColumn td=null;
		for(int i=0;i<tdList.size();i++){
			Node n=tdList.elementAt(i);
			if(n!=null && n instanceof org.htmlparser.tags.TableColumn){
				td=(org.htmlparser.tags.TableColumn)n;
				break;
			}
		}
		Div div=null;
		NodeList divList=td.getChildren();
		for(int i=0;i<divList.size();i++){
			Node n=divList.elementAt(i);
			if(n!=null && n instanceof Div){
				div=(Div)n;
				break;
			}
		}
		if(div==null)return;
		String id=div.getAttribute("id");
		if(StringUtils.isEmpty(id))return;
		HttpServletRequest request=DoradoContext.getCurrent().getRequest();
		String taskId=request.getParameter("taskId");
		if(StringUtils.isEmpty(taskId))return;
		Task task=taskClient.getTask(Long.valueOf(taskId));
		HistoryTaskQuery query=historyService.createHistoryTaskQuery();
		query.rootProcessInstanceId(task.getRootProcessInstanceId());
		query.addOrderDesc("endDate");
		List<HistoryTask> hisTasks=query.list();
		Grid grid=new Grid();
		DataColumn nameCol=buildColumn("taskName", "任务名称");
		nameCol.setWidth("220");
		grid.addColumn(nameCol);
		DataColumn dateCol=buildColumn("endDate", "处理日期");
		dateCol.setWidth("180");
		dateCol.setDisplayFormat("Y-m-d H:i:s");
		grid.addColumn(dateCol);
		DataColumn assigneeCol=buildColumn("assignee", "处理人");
		assigneeCol.setWidth("120");
		grid.addColumn(assigneeCol);
		grid.addColumn(buildColumn("opinion", "处理意见"));
		grid.setItems(hisTasks);
		grid.setRenderOn("#"+id);
		grid.setHeight("200");
		grid.setReadOnly(true);
		view.addChild(grid);
	}
	
	private DataColumn buildColumn(String name,String label){
		DataColumn col=new DataColumn();
		col.setName(name);
		col.setCaption(label);
		return col;
	}
}
